package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.MyLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    @Operation(summary = "上传声音")
    @PostMapping("uploadTrack")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        return Result.ok(vodService.uploadTrack(file));
    }

    @Operation(summary = "保存/新增声音")
    @PostMapping("saveTrackInfo")
    @MyLogin
    //@Validated让实体类中的 判断是否为空类的注解生效
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    @MyLogin(required = true)
    @Operation(summary = "查询声音列表")
    @PostMapping("findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            @Parameter(name = "albumInfoQuery", description = "查询对象", required = false)
            @RequestBody TrackInfoQuery trackInfoQuery) {
        return Result.ok(trackInfoService.findUserTrackPage(new Page<TrackListVo>(page, limit), trackInfoQuery));
    }

    @Operation(summary = "删除声音信息")
    @DeleteMapping("removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        // 调用服务层方法
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    @Operation(summary = "修改声音")
    @PutMapping("updateTrackInfo/{id}")
    public Result updateById(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        // 调用服务层方法
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    @MyLogin(required = false)
    @Operation(summary = "获取专辑声音分页列表")
    @GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable("albumId") Long albumId,
                                                              @PathVariable("page") Long page,
                                                              @PathVariable("limit") Long limit) {
        //获取userId
        Long userId = AuthContextHolder.getUserId();
        //创建page对象，设置当前页和每页记录数
        Page<AlbumTrackListVo> pageParam = new Page<>(page, limit);
        return Result.ok(trackInfoService.findAlbumTrackPage(pageParam, albumId, userId));
    }

    @Operation(summary = "获取声音信息")
    @GetMapping("getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        //	调用服务层方法
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    @Operation(summary = "批量获取下单付费声音列表")
    @GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
    Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount) {
        return Result.ok(trackInfoService.findPaidTrackInfoList(trackId, trackCount));
    }

    @MyLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable("trackId") Long trackId) {
        // 获取购买记录集合
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> map = trackInfoService.findUserTrackPaidList(trackId, userId);
        return Result.ok(map);
    }

}

